package com.dao.user;

import com.enitiy.User;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.utils.page.Page;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * 创建作者 ：MySunshine
 * 创建时间 2022/10/24 11:10
 * 项目名称 SHUSHAN
 */
public class UserDaoImpl implements UserDao {
    QueryRunner qr = new QueryRunner(new ComboPooledDataSource());

    @Override  //用户登录
    public User UserLogin(int uNum, String pwd) throws SQLException {
        String sql="select * from user where unum=? and pwd=?";
        return  qr.query(sql, new BeanHandler<User>(User.class), uNum, pwd);
    }

    @Override//用户注册
    public boolean UserReg(User u) throws SQLException {
        String sql="insert into user(unum,name,pwd,age,sex,phone,startime,kid) value(?,?,?,?,?,?,?,?)";
        int update = qr.update(sql, u.getUnum(), u.getName(), u.getPwd(), u.getAge(), u.getSex(), u.getPhone(), u.getStartime(),u.getKid());
        return update>=1?true:false;
    }

    @Override//用户注册 通过账号上传头像
    public boolean UserGetImgByNum(int num, String img) throws SQLException {
        String sql="update user set img=? where unum=?";
        int update = qr.update(sql, img,num);
        return update>=1?true:false;
    }

    @Override//用户查询
    public List<User> UserSelectAll(Page p) throws SQLException {
        String sql="select  *  from  user where 1=1";
        if (p.getName()!=null&&!p.getName().equals("")){
            sql+=" and name like'%"+p.getName()+"%'";
        }
        sql+="  limit ?,?";
        int pageSize = p.getPageSize();
        int startIndex= (p.getPageNum()-1)*pageSize;
        return  qr.query(sql,new BeanListHandler<User>(User.class),startIndex, pageSize);
    }

    @Override//查询用户条数
    public int UserSelectAllNum(Page p) throws SQLException {
        String sql="select count(*) from user where  1=1 ";
        if (p.getName()!=null&&!p.getName().equals("")){
            sql+=" and name like'%"+p.getName()+"%'";
        }
        long count = qr.query(sql, new ScalarHandler<Long>());
        return (int)count;
    }

    @Override//删除用户
    public boolean UserDeleteByNum(int num) throws SQLException {
        String sql= "delete from user where unum = ?";
        int update = qr.update(sql, num);
        return  update>=1?true:false;
    }

    @Override//修改
    public boolean UserUpdateByNum(User u) throws SQLException {
        String sql="update  user set name=?,age=? ,sex=?,phone=?,kid=?,pwd=? where unum=?";
        int update = qr.update(sql,u.getName(),u.getAge(),u.getSex(),u.getPhone(),u.getKid(),u.getPwd(),u.getUnum());
        return update>=1?true:false;
    }

    @Override//根据num查询用户
    public User UserSelectByNum(int Num) throws SQLException {
      String sql=" select * from user where unum=?" ;
        return   qr.query(sql,new BeanHandler<User>(User.class),Num);

    }
}
